package com.library.controller;

import com.library.bean.User;
import com.library.service.UserService;
import com.library.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @Author : 雾眠气泡水
 * @Date : 2024/9/15 -- 01:06
 * @Description :
 */

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        UserService us = new UserServiceImpl();
        String user_account = req.getParameter("userAccount");

        String pass_word = req.getParameter("passWord");

        Map<String, Object> map = us.user$login(user_account, pass_word);
        if(map.get("success")!=null){
            User user = (User)map.get("success");
            HttpSession session = req.getSession();
            session.setAttribute("logUser",user);
            req.getRequestDispatcher("books").forward(req,resp);
        }else{
            String error = (String) map.get("fail");
            req.setAttribute("error",error);
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
    }
}
